package com.onLineCar.driver.controller;


import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.onLineCar.common.core.domain.Result;
import com.onLineCar.driver.domain.entity.Discount;
import com.onLineCar.driver.domain.entity.Request.*;
import com.onLineCar.driver.domain.entity.TDriver;
import com.onLineCar.driver.domain.entity.Valuation;
import com.onLineCar.driver.domain.entity.Wallet;
import com.onLineCar.driver.dto.DriverInfoDTO;
import com.onLineCar.driver.service.ITDriverService;
import com.onLineCar.driver.util.OCRUtils;
import com.onLineCar.driver.util.OssUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


/**
 * 司机管理服务
 *
 * @author dsr
 * @since 2023-12-12
 */
@RestController
@RequestMapping("/driver")
@Slf4j
@Api(tags = "阿里云接口服务")
public class TDriverController {

    @Autowired
    private ITDriverService iDriverService;
    @Autowired
    private HttpServletRequest request;


    /**
     * 司机注册
     *
     * @param tDriver
     * @return
     */
    @PostMapping("/registerDriver")
    public Result registerDriver(@RequestBody TDriver tDriver) {
        log.info("功能名称:{司机注册},请求方法:{},请求URL:{},请求参数:{}",
                request.getMethod(), request.getRequestURL(), tDriver);
        int i = iDriverService.registerDriver(tDriver);
        log.info("功能名称:{司机注册},请求方法:{},请求URL:{},响应结果:{}",
                request.getMethod(), request.getRequestURL(), i);
        return Result.success(i);
    }


    /**
     * OssUtils工具类
     * oss图片上传
     *
     * @param file
     * @return
     */

    @Primary
    @PostMapping("/upLoad")
    public Result upLoad(@RequestParam("file") MultipartFile file) {

        log.info("功能名称:{上传图片},请求方法:{},请求URL:{},请求参数:{}",
                request.getMethod(), request.getRequestURL(), file);
        String url = OssUtil.uploadMultipartFile(file);
        log.info("功能名称:{上传图片},请求方法:{},请求URL:{},响应结果:{}",
                request.getMethod(), request.getRequestURL(), url);
        return Result.success(url);
    }

//    //uploadFile
//    @PostMapping("/upLoad")
//    public Result upLoad(MultipartFile file) {
//        log.info("功能名称:{上传图片},请求方法:{},请求URL:{},请求参数:{}",
//                request.getMethod(), request.getRequestURL(), file);
//        //String url = OssUtil.uploadMultipartFile(file);
//        Result<SysFile> url = goodGuang.upload(file);
//        log.info("功能名称:{上传图片},请求方法:{},请求URL:{},响应结果:{}",
//                request.getMethod(), request.getRequestURL(), url);
//        return Result.success(url);
//    }


    /**
     * 根据身份证号查询司机是否已注册
     *
     * @param driverIdcard
     * @return
     */
    @PostMapping("/queryDriverByIdCard/{driverIdcard}")
    public Result<TDriver> queryDriverByIdCard(@PathVariable String driverIdcard) {
        log.info("功能名称:{查询是否已注册},请求方法:{},请求URL:{},请求参数:{}",
                request.getMethod(), request.getRequestURL(), driverIdcard);
        LambdaQueryWrapper<TDriver> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(StringUtils.isNotBlank(driverIdcard), TDriver::getDriverIdcard, driverIdcard);
        TDriver queryDriverByIdCard = iDriverService.queryDriverByIdCard(queryWrapper);
        log.info("功能名称:{查询是否已注册},请求方法:{},请求URL:{},响应结果:{}",
                request.getMethod(), request.getRequestURL(), queryDriverByIdCard);
        return Result.success(queryDriverByIdCard);
    }

    /**
     * 根据审核状态查询司机集合
     *
     * @param reqCheck
     * @return
     */
    @PostMapping("/queryDriverByState")
    public Result<IPage<TDriver>> queryDriverByState(@RequestBody ReqCheck reqCheck) {
        log.info("功能名称:{查询待审核司机},请求方法:{},请求URL:{},请求参数:{}",
                request.getMethod(), request.getRequestURL(), reqCheck);
        IPage<TDriver> queryDriverByState = iDriverService.queryDriverByState(reqCheck);
        Result result = Result.success(queryDriverByState);
        log.info("功能名称:{查询待审核司机},请求方法:{},请求URL:{},响应结果:{}",
                request.getMethod(), request.getRequestURL(), result);
        return result;
    }

    /**
     * 根据Id查看司机详情 查询
     *
     * @param driverId
     * @return
     */
    @PostMapping("/queryDriverListByDriverId/{driverId}")
    public Result<TDriver> queryDriverListByDriverId(@PathVariable Integer driverId) {
        log.info("功能名称:{查看司机详情},请求方法:{},请求URL:{},请求参数:{}",
                request.getMethod(), request.getRequestURL(), driverId);
        TDriver tDriver = iDriverService.queryDriverListByDriverId(driverId);
        log.info("功能名称:{查询待审核司机},请求方法:{},请求URL:{},响应结果:{}",
                request.getMethod(), request.getRequestURL(), tDriver);
        return Result.success(tDriver);
    }

    /**
     * 审核司机
     *
     * @param requestDriver
     * @return
     */
    @PostMapping("/updateDriverStateById")
    public Result updateDriverStateById(@RequestBody RequestDriver requestDriver) {
        log.info("功能名称:{审核司机},请求方法:{},请求URL:{},请求参数:{}",
                request.getMethod(), request.getRequestURL(), requestDriver);
        int i = iDriverService.updateDriverStateById(requestDriver);
        log.info("功能名称:{审核司机},请求方法:{},请求URL:{},响应结果:{}",
                request.getMethod(), request.getRequestURL(), i);
        return Result.success(i);
    }

    /**
     * 根据审核状态查询司机集合
     *
     * @param requestSelect
     * @return
     */
    @PostMapping("/queryDriverByCheck")
    public Result<IPage<TDriver>> queryDriverByCheck(@RequestBody RequestSelect requestSelect) {
        log.info("功能名称:{根据审核状态查询司机集合},请求方法:{},请求URL:{},请求参数:{}",
                request.getMethod(), request.getRequestURL(), requestSelect);
        Page<TDriver> page = new Page<>(requestSelect.getPageNum(), requestSelect.getPageSize());
        LambdaQueryWrapper<TDriver> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(requestSelect.getDriverName()), TDriver::getDriverName, requestSelect.getDriverName());
        queryWrapper.eq(requestSelect.getCheckState() != null, TDriver::getCheckState, requestSelect.getCheckState());
        IPage<TDriver> result = iDriverService.page(page, queryWrapper);
        log.info("功能名称:{根据审核状态查询司机集合},请求方法:{},请求URL:{},响应结果:{}",
                request.getMethod(), request.getRequestURL(), result);
        return Result.success(result);
    }

    /**
     * 抢单接口
     *
     * @param preemptOrder
     * @return
     */
    @PostMapping("/preemptOrder")
    public Result preemptOrder(@RequestBody RequestPreempt preemptOrder) {
        log.info("功能名称:{抢单接口},请求方法:{},请求URL:{},请求参数:{}",
                request.getMethod(), request.getRequestURL(), preemptOrder);
        boolean result = iDriverService.preemptOrder(preemptOrder);
        log.info("功能名称:{抢单接口},请求方法:{},请求URL:{},响应结果:{}",
                request.getMethod(), request.getRequestURL(), result);
        if (result) {
            return Result.success("抢单成功");
        } else {
            return Result.success("抢单失败");
        }
    }

    /**
     * 查询司机列表
     *
     * @return
     */
    @GetMapping("/queryDriverList")
    public Result<List<TDriver>> queryDriverList() {
        log.info("功能名称:{查询司机列表},请求方法:{},请求URL:{},请求参数:{}",
                request.getMethod(), request.getRequestURL());
        List<TDriver> queryDriverList = iDriverService.queryDriverList();
        log.info("功能名称:{查询司机列表},请求方法:{},请求URL:{},响应结果:{}",
                request.getMethod(), request.getRequestURL(), queryDriverList);
        return Result.success(queryDriverList);
    }

    /**
     * 根据名称查询司机信息
     */
    @PostMapping("/selectDriverByName")
    public Result<TDriver> selectDriverByName(@RequestParam String driverName) {
        log.info("功能名称:{根据名称查询司机信息},请求方法:{},请求URL:{},请求参数:{}",
                request.getMethod(), request.getRequestURL(), driverName);
        LambdaQueryWrapper<TDriver> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(StringUtils.isNotBlank(driverName), TDriver::getDriverName, driverName);
        TDriver result = iDriverService.selectDriverByName(queryWrapper);
        log.info("功能名称:{根据名称查询司机信息},请求方法:{},请求URL:{},响应结果:{}",
                request.getMethod(), request.getRequestURL(), result);
        return Result.success(result);
    }

    /**
     * 根据订单计价
     *
     * @param requestValuation
     * @return
     */
    @PostMapping("/valuationOrderByNumber")
    public Result<Valuation> valuationOrderByNumber(@RequestBody RequestValuation requestValuation) {
        log.info("功能名称:{根据订单计价},请求方法:{},请求URL:{},请求参数:{}",
                request.getMethod(), request.getRequestURL(), requestValuation);
        Valuation valuationOrderByNumber = iDriverService.valuationOrderByNumber(requestValuation);
        log.info("功能名称:{根据订单计价},请求方法:{},请求URL:{},响应结果:{}",
                request.getMethod(), request.getRequestURL(), valuationOrderByNumber);
        return Result.success(valuationOrderByNumber);
    }

    /**
     * 根据车辆id查询司机信息如果司机存在，则返回司机信息 如果司机不存在，则返回false
     *
     * @param carIds
     * @return
     */

    @PostMapping("/selectDriverByCarIds")
    public List<DriverInfoDTO> selectDriverByCarId(@RequestBody List<Integer> carIds) {
        log.info("功能名称:{根据车辆id查询司机信息},请求方法:{},请求URL:{},请求参数:{}",
                request.getMethod(), request.getRequestURL(), carIds);
        LambdaQueryWrapper<TDriver> driverLambdaQueryWrapper = new LambdaQueryWrapper<TDriver>()
                .in(TDriver::getCarId, carIds);
        DriverInfoDTO driverInfoDTO = new DriverInfoDTO();
        return driverInfoDTO.convertDriverInfoList(iDriverService.list(driverLambdaQueryWrapper), carIds);
    }

    /**
     * 司机Redis分布式锁抢单
     *
     * @param ReqDriverPreemptOrder
     * @return
     */
    @PostMapping("/driverPreemptOrder")
    public Result driverPreemptOrder(@RequestBody ReqDriverPreemptOrder ReqDriverPreemptOrder) {
        log.info("功能名称:{司机Redis分布式锁抢单},请求方法:{},请求URL:{},请求参数:{}",
                request.getMethod(), request.getRequestURL(), ReqDriverPreemptOrder);
        boolean result = iDriverService.driverPreemptOrder(ReqDriverPreemptOrder);
        log.info("功能名称:{司机Redis分布式锁抢单},请求方法:{},请求URL:{},响应结果:{}",
                request.getMethod(), request.getRequestURL(), result);
        if (result) {
            return Result.success("抢单 成功");
        } else {
            return Result.success("抢单失败");
        }
    }

    /**
     * 司机redisson分布式锁抢单
     *
     * @param reqDriverPreemptOrder
     * @return
     */
    @PostMapping("/redissonDriverPreempt")
    public Result redissonDriverPreempt(@RequestBody ReqDriverPreemptOrder reqDriverPreemptOrder) {
        log.info("功能名称:{司机redisson分布式锁抢单},请求方法:{},请求URL:{},请求参数:{}",
                request.getMethod(), request.getRequestURL(), reqDriverPreemptOrder);
        boolean result = iDriverService.redissonDriverPreempt(reqDriverPreemptOrder);
        log.info("功能名称:{司机Redis分布式锁抢单},请求方法:{},请求URL:{},响应结果:{}",
                request.getMethod(), request.getRequestURL(), result);
        if (result) {
            return Result.success("抢单成功");
        } else {
            return Result.success("抢单失败");
        }
    }

    /**
     * 查询钱包信息
     *
     * @param walletId
     * @return
     */
    @PostMapping("/selectWalletInfo/{walletId}")
    public Result<List<Wallet>> selectWalletInfo(@PathVariable Integer walletId) {
        log.info("功能名称:{查询钱包信息},请求方法:{},请求URL:{},请求参数:{}",
                request.getMethod(), request.getRequestURL(), walletId);
        List<Wallet> selectWalletInfo = iDriverService.selectWalletInfo(walletId);
        log.info("功能名称:{查询钱包信息},请求方法:{},请求URL:{},响应结果:{}",
                request.getMethod(), request.getRequestURL(), selectWalletInfo);
        return Result.success(selectWalletInfo);
    }

    /**
     * 查询钱包对应优惠券
     *
     * @param walletId
     * @return
     */
    @PostMapping("/selectDiscountInfo/{walletId}")
    public Result<List<Discount>> selectDiscountInfo(@PathVariable Integer walletId) {
        log.info("功能名称:{查询折扣信息},请求方法:{},请求URL:{},请求参数:{}",
                request.getMethod(), request.getRequestURL(), walletId);
        List<Discount> selectDiscountInfo = iDriverService.selectDiscountInfo(walletId);
        log.info("功能名称:{查询折扣信息},请求方法:{},请求URL:{},响应结果:{}",
                request.getMethod(), request.getRequestURL(), selectDiscountInfo);
        return Result.success(selectDiscountInfo);
    }

    /**
     * ocr身份证图片内容提取
     *
     * @param imageUrl
     * @return
     */
    @RequestMapping("upLoadOcr")
    public Result upLoadOcr(@RequestParam String imageUrl) {
        log.info("功能名称:{ocr图片内容提取},请求方法:{},请求URL:{},请求参数:{}",
                request.getMethod(), request.getRequestURL(), imageUrl);
        try {
            Map<String, String> idCardInfo = OCRUtils.parseIdCardInfo(imageUrl);
            log.info("功能名称:{ocr图片内容提取},请求方法:{},请求URL:{},响应结果:{}",
                    request.getMethod(), request.getRequestURL(), idCardInfo);
            return Result.success(idCardInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("发生错误");
    }


    /**
     * 初始化钱包  添加
     * @param wallet
     * @return
     */
    @PostMapping("/insertWallet")
    public Result<Wallet> insertWallet(@RequestBody Wallet wallet) {
        log.info("功能名称:{初始化钱包添加},请求方法:{},请求URL:{},请求参数:{}",
                request.getMethod(), request.getRequestURL(), wallet);
        iDriverService.insertWallet(wallet);
        log.info("功能名称:{初始化钱包添加},请求方法:{},请求URL:{}",
                request.getMethod(), request.getRequestURL());
        return Result.success();
    }


    /**
     * 钱包余额充值
     * @param reqWallet
     * @return
     */
    @PostMapping("/insertWalletPrice")
    public Result<Wallet> insertWalletPrice(@RequestBody ReqWallet reqWallet) {
        log.info("功能名称:{钱包余额充值},请求方法:{},请求URL:{},请求参数:{}",
                request.getMethod(), request.getRequestURL(), reqWallet);
        iDriverService.insertWalletPrice(reqWallet);
        log.info("功能名称:{钱包余额充值},请求方法:{},请求URL:{}",
                request.getMethod(), request.getRequestURL());
        return Result.success();
    }

    /**
     * 用户余额付款
     * @param walletPay
     * @return
     */
    @PostMapping("/WalletPayMoney")
    public Result WalletPayMoney(@RequestBody WalletPay walletPay){
        log.info("功能名称:{用户余额付款},请求方法:{},请求URL:{},请求参数:{}",
                request.getMethod(), request.getRequestURL(), walletPay);
        String result = iDriverService.WalletPayMoney(walletPay);
        log.info("功能名称:{用户余额付款},请求方法:{},请求URL:{},响应结果:{}",
                request.getMethod(), request.getRequestURL(), result);
        return Result.success(result);
    }



}
